﻿module Sustainalytics.Controversies {

    export class ControversiesService implements IControversiesService {

        constructor(private $q: ng.IQService, private $http: ng.IHttpService) { }
        

        public getQualitativePerformance(companyId: string): ng.IPromise<ControversyIndicatorDto[]> {
            var ctx = this;

            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/' + companyId+'?qualitativeperformance=true';

            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);

            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getComparisonGroupControversies(companyId: string): ng.IPromise<ComparisonGroupControversyDto[]> {
            var ctx = this;

            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/'+companyId+'/comparisonGroupControversies?level=0';
 
            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);

            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<ComparisonGroupControversyDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<ComparisonGroupControversyDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getControversyIndicators(companyId: string, theme: string): ng.IPromise<ControversyIndicatorDto[]> {
            var ctx = this;

            var params = $.param(
                {
                    theme: theme
                });

            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator?' + params;

            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);
            
            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getEventIndicator(companyId: string, eventIndicatorId: string): ng.IPromise<EventIndicatorDto> {
            var ctx = this;

            var controversyIndicatorId: string = '00000000-0000-0000-0000-000000000000';

            var url = '/controversiesserv/api/v1/company/' + companyId +
                '/controversyIndicator/' + controversyIndicatorId +
                '/eventIndicator/' + eventIndicatorId;


            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);

            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<EventIndicatorDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<EventIndicatorDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getIncidents(companyId: string, eventIndicatorId: string): ng.IPromise<IncidentDto[]> {
            var ctx = this;

            var controversyIndicatorId: string = '00000000-0000-0000-0000-000000000000';
            var incidentId: string = '00000000-0000-0000-0000-000000000000';

            var url = '/controversiesserv/api/v1/company/' + companyId +
                '/controversyIndicator/' + controversyIndicatorId +
                '/eventIndicator/' + eventIndicatorId +
                '/incident/';

            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);

            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<IncidentDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<IncidentDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }


        public getIncidentsMapLocationForCompany(companyId: string): ng.IPromise<MapDataDto[]> {
            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/eventIndicator/incident?=IncludeHQ=true&Location=true&Type=0';
            var deferred = this.$q.defer();
            var promise = this.$http.get(url);
            promise.then(succes => {
                deferred.resolve(succes.data);
            }, error => {
                deferred.reject(error);
                });
            return deferred.promise;
        }

        public getIncidentsMapLocationByEventIndicator(companyId: string, eventIndicator:string): ng.IPromise<MapDataDto[]> {
            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/'+companyId+'/eventIndicator/'+eventIndicator+'/incident?=IncludeHQ=false&Location=true&Type=0';
            var deferred = this.$q.defer();
            var promise = this.$http.get(url);
            promise.then(succes => {
                deferred.resolve(succes.data);
            }, error => {
                    deferred.reject(error);
                });
            return deferred.promise;
        }
    }


    export var controversersiesServiceName = 'ControversiesService';

    angular.module(controversiesModuleName).service(controversersiesServiceName,
        ['$q', '$http', ControversiesService]);
} 